package com.mrlv.ht.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mrlv.ht.mapper.ModuLeMapper;
import com.mrlv.ht.mapper.RoleMapper;
import com.mrlv.ht.mapper.RoleModuleMapper;
import com.mrlv.ht.pojo.ModuLe;
import com.mrlv.ht.pojo.Role;
import com.mrlv.ht.pojo.RoleExample;
import com.mrlv.ht.pojo.RoleModuleExample;
import com.mrlv.ht.pojo.RoleModuleExample.Criteria;
import com.mrlv.ht.pojo.RoleModuleKey;
import com.mrlv.ht.service.IRoleService;

@Service("roleService")
public class RoleServiceImpl implements IRoleService {

	@Resource
	private RoleMapper roleMapper;
	@Resource
	private ModuLeMapper moduLeMapper;
	@Resource
	private RoleModuleMapper roleModuLeMapp;
	
	@Override
	public int insertSelective(Role record) {
		record.setRoleId(UUID.randomUUID().toString());
		record.setCreateTime(new Date());
		record.setUpdateTime(record.getCreateTime());
		return roleMapper.insert(record);
	}

	@Override
	public int updateByPrimaryKeySelective(Role record) {
		record.setUpdateTime(new Date());
		return roleMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<Role> selectByExample(RoleExample example) {
		return roleMapper.selectByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(String roleId) {
		roleModuLeMapp.deleteRole(new String []{roleId});
		return roleMapper.deleteByPrimaryKey(roleId);
	}

	@Override
	public String getJson(String roleId) {
		ObjectMapper mapper = new ObjectMapper();
		List<ModuLe> moduLes = moduLeMapper.findAll();
		System.out.println(moduLes);
		RoleModuleExample rme = new RoleModuleExample();
		Criteria rmeCriteria = rme.createCriteria();
		rmeCriteria.andRoleIdEqualTo(roleId);
		List<RoleModuleKey> rms = roleModuLeMapp.selectByExample(rme);
		for(int i=0; i<moduLes.size(); i++) {
			ModuLe m = moduLes.get(i);
			m.setId(m.getModuleId());
			m.setpId(m.getParentId());
			for (int j=0; j<rms.size(); j++) {
				String s = rms.get(j).getModuleId();
				if (s.equals(m.getId())) {
					m.setChecked(true);
				}
			}
		}
		try {
			String json = mapper.writeValueAsString(moduLes);
			return json;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int insertModuLeRole(RoleModuleKey rm) {
		return roleModuLeMapp.insertSelective(rm);
	}

	@Override
	public int deleteBatch(String[] array) {
		roleModuLeMapp.deleteRole(array);
		return roleMapper.deleteBatch(array);
	}

	@Override
	public Role selectByPrimaryKey(String roleId) {
		return roleMapper.selectByPrimaryKey(roleId);
	}

	@Override
	public String getJson() {
		ObjectMapper mapper = new ObjectMapper();
		List<ModuLe> moduLes = moduLeMapper.findAll();
		for(int i=0; i<moduLes.size(); i++) {
			ModuLe m = moduLes.get(i);
			m.setId(m.getModuleId());
			m.setpId(m.getParentId());
		}
		try {
			String json = mapper.writeValueAsString(moduLes);
			return json;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<RoleModuleKey> selectByExample(RoleModuleExample example) {
		return roleModuLeMapp.selectByExample(example);
	}

	@Override
	public int deleteByPrimaryKeyByModule(String moduleId) {
		return roleModuLeMapp.deleteModuLe(new String[] {moduleId});
	}
	

}
